# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libmariadbclient
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.13
pkgrel=1
pkgdesc="MariaDB client libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://mariadb.org/"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-tools-git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-curl"
         #"${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib")
#optdepends=("${MINGW_PACKAGE_PREFIX}-curl")
options=('!strip' 'staticlibs')
source=(https://mirror.rackspace.com/mariadb/connector-c-${pkgver}/mariadb-connector-c-${pkgver}-src.tar.gz{,.asc}
        001-mingw-build.patch
        002-fix-prototype.patch
        003-gcc-fix-use_VA_ARGS.patch)
sha256sums=('0271a5edfd64b13bca5937267474e4747d832ec62e169fc2589d2ead63746875'
            'SKIP'
            'a5ef33cae8a5a5b06c5b5686628bb8e9419f6bd2305343c63e952f4da1fa0fac'
            '346695ce8f10c2c426f880240ef7bbe7b8d70c6e58b0cc30483735c2b2d53261'
            'fc08055e5d63e310e2658b15f6e1f00b59f2aad2dec37560c01954fac6af4a6e')
validpgpkeys=("199369E5404BD5FC7D2FE43BCBCB082A1BB943DB") #MariaDB Package Signing Key <package-signing-key@mariadb.org>

prepare() {
  cd ${srcdir}/mariadb-connector-c-${pkgver}-src
  patch -p1 -i ${srcdir}/001-mingw-build.patch
  patch -p1 -i ${srcdir}/002-fix-prototype.patch
  patch -p1 -i ${srcdir}/003-gcc-fix-use_VA_ARGS.patch
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DWITH_EXTERNAL_ZLIB=ON \
    -DWITH_MYSQLCOMPAT=OFF \
    -DWITH_SSL="SCHANNEL" \
    -DCLIENT_PLUGIN_AUTH_GSSAPI_CLIENT=STATIC \
    -DCLIENT_PLUGIN_DIALOG=STATIC \
    -DCLIENT_PLUGIN_REMOTE_IO=OFF \
    -DCLIENT_PLUGIN_PVIO_NPIPE=STATIC \
    -DCLIENT_PLUGIN_PVIO_SHMEM=STATIC \
    -DCLIENT_PLUGIN_CLIENT_ED25519=OFF \
    -DCLIENT_PLUGIN_CACHING_SHA2_PASSWORD=STATIC \
    -DCLIENT_PLUGIN_SHA256_PASSWORD=STATIC \
    -DCLIENT_PLUGIN_MYSQL_CLEAR_PASSWORD=STATIC \
    -DCLIENT_PLUGIN_MYSQL_OLD_PASSWORD=STATIC \
    ../mariadb-connector-c-${pkgver}-src

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  ln -s "${pkgdir}${MINGW_PREFIX}"/include/mariadb "${pkgdir}${MINGW_PREFIX}"/include/mysql

  if [ "${CARCH}" = "i686" ]; then
    rm -f ${pkgdir}${MINGW_PREFIX}/lib/libmariadb.dll.a
    ${MINGW_PREFIX}/bin/gendef -a ${pkgdir}${MINGW_PREFIX}/bin/libmariadb.dll
    ${MINGW_PREFIX}/bin/dlltool -k -d libmariadb.def -l ${pkgdir}${MINGW_PREFIX}/lib/libmariadb.dll.a
  fi

  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadb,libmysqlclient}.dll.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadb,libmysqlclient_r}.dll.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadbclient,libmysqlclient}.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadbclient,libmysqlclient_r}.a

  local _PRE_WIN="$(cygpath -m ${MINGW_PREFIX})"

  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PRE_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
}
